package com.ossjk.ossjkservlet.system.servlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ossjk.core.base.servlet.BaseServlet;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.ossjkservlet.common.Constant;
import com.ossjk.ossjkservlet.system.dao.UserDao;
import com.ossjk.ossjkservlet.system.pojo.User;

@WebServlet("/system/index.do")
public class IndexServlet extends BaseServlet {

	private UserDao userDao = new UserDao();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
		String method = getMethod(req);
		if ("toIndex".equals(method)) {
			toIndex(req, resp);
		} else if ("toLogin".equals(method)) {
			toLogin(req, resp);
		} else if ("login".equals(method)) {
			login(req, resp);
		} else if ("logout".equals(method)) {
			logout(req, resp);
		} else if ("toDesktop".equals(method)) {
			toDesktop(req, resp);
		} else if ("toError".equals(method)) {
			toError(req, resp);
		}
	}

	/**
	 * 登出
	 * 
	 * @param req
	 * @param resp
	 */
	public void logout(HttpServletRequest req, HttpServletResponse resp) {
		try {
			req.getSession().removeAttribute(Constant.SESSION_USER_KEY);
			forward(req, resp, "/WEB-INF/page/login.jsp");
		} catch (Exception e) {
			logger.error("", e);
			redirect(req, resp, req.getContextPath() + "/system/index.do?method=toError");
		}
	}

	/**
	 * 登录
	 * 
	 * @param req
	 * @param resp
	 */
	public void login(HttpServletRequest req, HttpServletResponse resp) {
		try {
			String jsessionid = null;
			String name = (String) req.getParameter("name");
			String pwd = (String) req.getParameter("pwd");
			String strrem = req.getParameter("rem");
			System.out.println("是否記住密碼" + strrem);
		//获取验证码
			String yzm = (String) req.getParameter("yzm");
			String yzmImg = (String) req.getSession().getAttribute("rand");
		//比较验证码
			//if (yzm != null) {
				if (yzmImg.equalsIgnoreCase(yzm)) {
					User user = userDao.selectOne("select * from user where name = ?  and pwd = ? ", name, pwd);
					if (!CommonUtil.isBlank(user)) {
						// 登录成功
						req.getSession().setAttribute(Constant.SESSION_USER_KEY, user);
						
						User user2 = new User();
						user2.setId(user.getId());
						// 更新ip
						user2.setLoginip(getIpAddr(req));
						// 以及登陆时间
						user2.setLogintime(CommonUtil.date6());
						userDao.updateBySelected(user2);
						//是否记住密码
						int rem = 0;
						if (strrem != null && !"".equals(strrem)) {
							rem = Integer.parseInt(strrem);
						}
						
						//七天免登陆
						Cookie[] cookies = req.getCookies();
						jsessionid = cookies[0].getValue();
						Cookie cookie = new Cookie("jsessionid", jsessionid);
						cookie.setMaxAge(60*60*24*7);
						resp.addCookie(cookie);
						
						
						//记住密码
						if (rem == 1) {
							Cookie cookieName = new Cookie("name", name);
							Cookie cookiePwd = new Cookie("pwd", pwd);
							System.out.println("=========进来了记住密码========");
							cookieName.setMaxAge(60*60*24*7);
							cookiePwd.setMaxAge(60*60*24*7);
							
							cookieName.setPath("/"); 
							cookiePwd.setPath("/"); 
							
							resp.addCookie(cookieName);
							resp.addCookie(cookiePwd);
							Cookie[] cookies1 = req.getCookies();
							int i = 0;
							for (Cookie cookie1 : cookies1) {
								System.out.println(i + " " + cookie1.getName() + " " + cookie1.getValue());
								i++;
							}
							
						}else {
							Cookie coolieName = new Cookie("name", null);
							Cookie cookiePwd = new Cookie("pwd", null);
							System.out.println("=========进来了不记住密码========");
							coolieName.setMaxAge(0);
							cookiePwd.setMaxAge(0);
							
							resp.addCookie(coolieName);
							resp.addCookie(cookiePwd);
						}
						
						//forward(req, resp, "/WEB-INF/page/index.jsp");
						redirect(req, resp, req.getContextPath() + "/system/index.do?method=toIndex");
					} else {
						// 登录失败
						req.setAttribute("msg", "登录失败");
						forward(req, resp, "/WEB-INF/page/login.jsp");
					}
				}else {
					// 验证码错误
					req.setAttribute("msg", "验证码错误");
					forward(req, resp, "/WEB-INF/page/login.jsp");
				}
//			} else {
//				req.setAttribute("msg", "请输入验证码");
//				forward(req, resp, "/WEB-INF/page/login.jsp");
//			}
			
			
		} catch (Exception e) {
			logger.error("", e);
			redirect(req, resp, req.getContextPath() + "/system/index.do?method=toError");
		}
	}

	/**
	 * 去登录
	 * 
	 * @param req
	 * @param resp
	 * @throws Exception
	 */
	public void toLogin(HttpServletRequest req, HttpServletResponse resp) {
		try {
			forward(req, resp, "/WEB-INF/page/login.jsp");
		} catch (Exception e) {
			logger.error("", e);
			redirect(req, resp, req.getContextPath() + "/system/index.do?method=toError");
		}
	}

	/**
	 * 去主页
	 * 
	 * @param req
	 * @param resp
	 */
	public void toIndex(HttpServletRequest req, HttpServletResponse resp) {
		try {
			forward(req, resp, "/WEB-INF/page/index.jsp");
		} catch (Exception e) {
			logger.error("", e);
			redirect(req, resp, req.getContextPath() + "/system/index.do?method=toError");
		}
	}

	/**
	 * 去桌面
	 * 
	 * @param req
	 * @param resp
	 */
	public void toDesktop(HttpServletRequest req, HttpServletResponse resp) {
		try {
			req.setAttribute("props", System.getProperties());
			forward(req, resp, "/WEB-INF/page/desktop.jsp");
		} catch (Exception e) {
			logger.error("", e);
			redirect(req, resp, req.getContextPath() + "/system/index.do?method=toError");
		}
	}

	/**
	 * 去错误页
	 * 
	 * @param req
	 * @param resp
	 */
	public void toError(HttpServletRequest req, HttpServletResponse resp) {
		forward(req, resp, "/WEB-INF/page/500.jsp");
	}

}
